//
//  YMFeedbackInputTextCell.m
//  SmartCar
//
//  Created by y s on 2022/11/18.
//

#import "YMFeedbackInputTextCell.h"

@interface YMFeedbackInputTextCell ()<UITextViewDelegate>

@property (nonatomic, strong) UIView *vBg;
@property (nonatomic, strong) UILabel *lblNumber;
@property (nonatomic, strong) UILabel *lblNumValue;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *lblPlace;
/** 输入最大个数 */
@property (nonatomic, assign) NSInteger maxNum;

@end

@implementation YMFeedbackInputTextCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - UI methods
- (void)setUI{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    _maxNum = 200;
    
    [self.contentView addSubview:self.vBg];
    [self.vBg addSubview:self.textView];
    [self.vBg addSubview:self.lblNumber];
    [self.vBg addSubview:self.lblNumValue];
    [self.textView addSubview:self.lblPlace];

    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.top.bottom.mas_equalTo(0);
    }];
    [self.lblNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.top.mas_equalTo([SFitUtil fit:17]);
    }];
    [self.lblNumValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lblNumber.mas_right);
        make.centerY.equalTo(self.lblNumber);
    }];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.top.mas_equalTo([SFitUtil fit:45]);
        make.bottom.mas_equalTo([SFitUtil fit:-15]);
    }];
    [self.lblPlace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.top.mas_equalTo([SFitUtil fit:8]);
    }];
    
    [self changeTextViewPlaceHolder];
    
}


#pragma mark - public methods
+ (CGFloat)getCellHeight{
    return [SFitUtil fit:200];
}


#pragma mark - Event Action

#pragma mark -- UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.lblPlace.hidden = YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    [self changeTextViewPlaceHolder];
}
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length > _maxNum) {
        textView.text = [textView.text substringToIndex:_maxNum];
    }
    if (self.changeTextBlock){
        self.changeTextBlock(textView.text);
    }
    self.lblNumValue.text = [NSString stringWithFormat:@"%ld/%ld",textView.text.length,_maxNum];
    [self changeTextViewPlaceHolder];
}
- (void)changeTextViewPlaceHolder{
    if (_textView.text.length > 0){
        self.lblPlace.hidden = YES;
    }else{
        self.lblPlace.hidden = NO;
    }
}

#pragma mark - setter and getter
- (UIView *)vBg{
    if (_vBg == nil) {
        _vBg = [[UIView alloc] init];
        _vBg.backgroundColor = STheme.D9;
        _vBg.clipsToBounds = YES;
        _vBg.layer.cornerRadius = [SFitUtil fit:8];
    }
    return _vBg;
}
- (UILabel *)lblNumber{
    if (_lblNumber == nil) {
        _lblNumber = [SThemeUtil boldLabel:KLanguage(@"feedback_lable_txt") color:STheme.H_D1 fontSize:16];
    }
    return _lblNumber;
}
- (UILabel *)lblNumValue{
    if (_lblNumValue == nil) {
        _lblNumValue = [SThemeUtil boldLabel:@"" color:STheme.H_D1 fontSize:16];
        _lblNumValue.text = [NSString stringWithFormat:@"0/%ld",_maxNum];
    }
    return _lblNumValue;
}
- (UILabel *)lblPlace{
    if (_lblPlace == nil) {
        _lblPlace = [SThemeUtil label:KLanguage(@"feedback_advice_txt") color:STheme.H_D4 fontSize:16];
        _lblPlace.hidden = YES;
    }
    return _lblPlace;
}
- (UITextView *)textView{
    if (_textView == nil){
        _textView = [[UITextView alloc] init];
        _textView.textColor = STheme.D1;
        _textView.font = [SFitUtil fitFont:16];
        _textView.returnKeyType = UIReturnKeyDone;
        _textView.keyboardType = UIKeyboardTypeDefault;
        _textView.delegate = self;
    }
    return _textView;
}


@end
